/**
 * 改成组建形式，该形式已废弃
 * @description:
 *   模态框的展示
 *
 * @author soulran(xr@ethercap.com)
 */

// modal function
function modal(action){
    switch(action){
        case 'hide':
            this[0].scrollTop = 0;
            this.removeClass("open");
            this.closest(".modal-opened").removeClass("modal-opened");
            this.trigger($.Event("hidden.xr.modal"));
            break;
        case 'self':
            this.addClass("open");
            this.parent().addClass("modal-opened");
            break;
        // case 'show':
        default:
            this.addClass("open");
            $("body").addClass("modal-opened");
            break;
    }
    return this;
}

module.exports = {
    fun: modal,
    init: function(){
        //隐藏modal
        $(document).on("click", "[data-dismiss=modal]", function(e){
            $(this).closest(".modal").modal("hide");
        });
        //清空modal
        $(document).on("hidden.xr.modal", ".modal-once-all", function(e){
            //reset所有的form
            $(this).find("form").each(function(i, form){
                form.reset();
                setTimeout(function(){$(form).trigger("reset").find("input,textarea").trigger("change");}, 0);
            });
        });

        $(document).on('click', '.modal', function(e){
            if(!$(e.target).closest('.modal-dialog').length){
                $('.modal').modal('hide');
            }
        });
    }
};
